package me.youline.dataServer.service.support.file;

import me.youline.dataServer.service.support.config.DisplayImageSizeConfig;
import me.youline.dataServer.service.support.file.FileBuilder.FileEntity;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 消息封面展示图定位器
 * @author linhan
 *
 */
@Component
public class DisplayPicFileLocator extends AbstractImgFileLocator<DisplayImageSizeConfig> implements NewFileLocatable<FileLocationConfig> {
	
	@Value("#{frontProperties['resource.fileCategory.displayPic']}")
	private String categoryName = "displayPic";
	
	@Autowired
	public DisplayPicFileLocator(DisplayImageSizeConfig config) {
		super(config);
	}

	@Override
	public FileEntity getNewFileEntity(FileLocationConfig config) {
		DateTime time = new DateTime();
		Md5Hash fileName = new Md5Hash(String.valueOf(time.getMillis()));
		return FileBuilder.newInstance(config.getInput()).root(this.getFileRoot())
				.dir(String.valueOf(time.getYear()))
				.dir(String.valueOf(time.getMonthOfYear()))
				.dir(String.valueOf(time.getDayOfMonth()))
				.file(fileName.toString()).getEntity();
	}

	@Override
	protected String getFileCategoryName() {
		return categoryName;
	}

}
